import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

let router = new Router({
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: () => import('@/view/Login')
    },
    {
      path: '/index',
      name: 'Index',
      component: () => import('@/view/Index')
    },
    {
      path: '/*',
      name: 'NotFound',
      component: () => import('@/view/NotFound')
    }
  ]
})
router.beforeEach((to, from, next) => {
  if(to.path === '/login'){
    //清空用户数据
    clearLoginInfo()
    if(from.path === '/login'){
      return false
    }
  }else{
    const token = sessionStorage.getItem('token');
    // const userName = sessionStorage.getItem('userName')
    if(token){



    }else{
      router.push('/login')
      return false
    }
  }
  next()
});
/**
 * 清除用户信息
 */
const clearLoginInfo=()=>{
  sessionStorage.removeItem('token')
}


export default router
